package com.angenela.imageshare.ui.my;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.angenela.imageshare.R;
import com.angenela.imageshare.activity.ChangePwd;
import com.angenela.imageshare.activity.LoginActivity;
import com.angenela.imageshare.activity.myCollect;
import com.angenela.imageshare.activity.myPush;
import com.angenela.imageshare.databinding.FragmentNotificationsBinding;
import com.bumptech.glide.Glide;


import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



public class NotificationsFragment extends Fragment {

    private NotificationsViewModel notificationsViewModel;
    private FragmentNotificationsBinding binding;
    ListView listView;
    ImageView imageView;
    TextView textView;
    Button button;

    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        notificationsViewModel = new ViewModelProvider(this).get(NotificationsViewModel.class);

        binding = FragmentNotificationsBinding.inflate(inflater, container, false);
        View root = binding.getRoot();

        textView = binding.Username;
        imageView = binding.imageView;
        button = binding.Exit;

        //用户名,头像获取
        Resources resources = getResources();
        String spFileNameKey = resources.getString(R.string.shared_preferences_file_name);
        String usernameKey = resources.getString(R.string.login_account_username);
        String avatarKey = resources.getString(R.string.login_account_avatar);

        SharedPreferences sharedPreferences = getActivity().getSharedPreferences(spFileNameKey, Context.MODE_PRIVATE);
        String username = sharedPreferences.getString(usernameKey,"");
        String avatar = sharedPreferences.getString(avatarKey,"");

        Glide.with(getContext()).load(avatar).into(imageView);
        textView.setText(username);


        listView = binding.notificationList;
        SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity(),getData(),R.layout.my_menu,new String[]{"title","image"},new int[]{R.id.myMenu_name,R.id.myMenu_image});
        listView.setAdapter(simpleAdapter);

//        final TextView textView = binding.textNotifications;
//        notificationsViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
//            @Override
//            public void onChanged(@Nullable String s) {
//                textView.setText(s);
//            }
//        });


        //修改密码等的页面转换
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String []Title = {"我发布的","我收藏的","修改密码"};
                int []image = {
                        R.drawable.ic_baseline_publish_24,
                        R.drawable.ic_baseline_star_24,
                        R.drawable.ic_baseline_lock_24};
                Bundle bundle = new Bundle();
                bundle.putInt("image",image[position]);
                bundle.putString("Title",Title[position]);

                if(position == 0){
                    Intent intent = new Intent(getActivity(), myPush.class);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }else if (position == 1){
                    Intent intent = new Intent(getActivity(), myCollect.class);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }else {
                    Intent intent = new Intent(getActivity(),ChangePwd.class);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }

            }
        });

        //退出登录
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), LoginActivity.class);
                startActivity(intent);
            }
        });

        return root;
    }


    private List<Map<String,Object>> getData(){
        String []Title = {"我发布的","我收藏的","修改密码"};
        int []image = {
                R.drawable.ic_baseline_publish_24,
                R.drawable.ic_baseline_star_24,
                R.drawable.ic_baseline_lock_24};
        List<Map<String,Object>> list = new ArrayList<>();
        for (int i = 0; i < 3; i++){
            Map map = new HashMap();
            map.put("title",Title[i]);
            map.put("image",image[i]);
            list.add(map);
        }

        return list;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }
}